﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EverBuy.SocialNetworking.BusinessObjects;
using EverBuy.SocialNetworking.Data.EntityFrameworkRepository.Abstracts;
using EverBuy.SocialNetworking.Data.Repository.Abstract;

namespace EverBuy.SocialNetworking.Data.EntityFrameworkRepository
{
    public class PermissionRepository : RepositoryBase, IPermissionRepository
    {
        public void DeletePermission(Permission permission)
        {
            DataContext.DeleteObject(ModelMapper.Mapper.Map(permission));
            DataContext.SaveChanges();
        }

        public void SavePermission(Permission permission)
        {
            if (permission.PermissionID > 0)
            {
                //update
            }
            else
            {
                //add
                DataContext.AddToPermissions(ModelMapper.Mapper.Map(permission));
            }
            DataContext.SaveChanges();
        }

        public List<Permission> GetPermissionById(int permissionId)
        {
            var permissions = from p in DataContext.Permissions
                              where p.PermissionID == permissionId
                              select p;
            return Enumerable.Select((from Permission permission in permissions select ModelMapper.Mapper.Map(permission)), dummy => ModelMapper.Mapper.Map((DataModel.Permission)dummy)).ToList();
        }

        public List<Permission> GetPermissionByName(string name)
        {
            var permissions = from p in DataContext.Permissions
                              where p.Name == name
                              select p;

            return Enumerable.Select((from Permission permission in permissions select ModelMapper.Mapper.Map(permission)), dummy => ModelMapper.Mapper.Map((DataModel.Permission)dummy)).ToList();
        }

        public List<Permission> GetPermissionsByAccountID(int accountId)
        {
            var permissions = from p in DataContext.Permissions
                              join ap in DataContext.AccountPermissions
                                      on p.PermissionID equals ap.Permission.PermissionID
                              join a in DataContext.Accounts
                                      on ap.Account.AccountID equals a.AccountID

                              where a.AccountID == accountId
                              select p;
            return permissions.Select(permission => ModelMapper.Mapper.Map(permission)).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="account"></param>
        /// <param name="permission"></param>
        public void AddPermission(Account account, Permission permission)
        {
            var ap = new AccountPermission
                         {
                             AccountID = account.ID,
                             PermissionID = permission.PermissionID
                         };

            DataContext.AddToAccountPermissions(ModelMapper.Mapper.Map(ap));
            DataContext.SaveChanges();
        }
    }
}
